package Servlet.Blog.controller;

import Servlet.Blog.entity.User;
import Servlet.Blog.service.UserService;
import Servlet.Blog.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author liyy
 * @description
 * @time 2020/10/13 22:57
 */
@WebServlet(name = "UsersServlet")
public class UsersServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    UserService userService = null;

    public void init() throws ServletException
    {
        // 执行必需的初始化
        userService = new UserServiceImpl();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置响应内容类型
        response.setContentType("text/html");

        // 实际的逻辑是在这里
        PrintWriter out = response.getWriter();
        //response.setCharacterEncoding("utf-8");

        List<User> userList1 = userService.queryUserList();

        String title1 = "用户";
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out1 = response.getWriter();

        String docType1 = "<!DOCTYPE html>\n";
        out1.println(docType1 +
                "<html> <head><title>" + title1 + "</title></head>\n" +
                "<body>");
        out1.println("<h3>用户信息</h3><table>");

        for (User u : userList1) {
            out1.println("id:"+u.getId());
            out1.println("name:"+u.getUsername());
            out1.println("status:"+u.getStatus());
        }
        out1.println("</table>");

        out1.println("</body></html>");
    }
}
